#!/usr/bin/env python

import json
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import sys


if len(sys.argv) != 2:
    print "please input the feedback dump file"
    sys.exit(-1);

times = []
offsets0 = []
offsets1 = []
thresholds0 = []
thresholds1 = []
var_noises0 = []
var_noises1 = []
incomings = []
targets = []
usages = []

datas = json.load(open(sys.argv[1]))
for data in datas:
    print 'loaded ', data
    times.append(data['time'])
    offsets0.append(data['offset-0'])
    offsets1.append(data['offset-1'])
    thresholds0.append(data['threshold-0'])
    thresholds1.append(data['threshold-1'])
    var_noises0.append(data['var_noise-0'])
    var_noises1.append(data['var_noise-1'])
    incomings.append(data['incoming'])
    targets.append(data['target'])
    usages.append(data['usage'])

#    ests.append(data['est'])
#    dts.append(data['dt'])
#    dss.append(data['ds'])

print len(times)
print len(offsets0)
print len(offsets1)
hist_res = [500,150]

plt.figure(1)

ax1 = plt.subplot(4,1,1)
ax1t = ax1.twinx()

ax1.scatter(times, offsets0, label='offset')
ax1.plot(times, thresholds0, 'r--', label='threshold')
ax1t.plot(times, var_noises0, 'm:', label='var-noise')

ax1.legend()
ax1.grid(True)

ax2 = plt.subplot(4,1,2)
ax2t = ax2.twinx()

ax2.scatter(times, offsets1, label='offset')
ax2.plot(times, thresholds1, 'r--', label='threshold')
ax2t.plot(times, var_noises1, 'm:', label='var-noise')

ax2.legend()
ax2t.legend()
ax2.grid(True)


ax3 = plt.subplot(4,1,3)
incoming_line, = ax3.plot(times, incomings, label='incoming')
target_line, = ax3.plot(times, targets, 'r--', label='target' )
ax3.legend()
ax3.grid(True)

ax4 = plt.subplot(4,1,4)
ax4.scatter(times, usages, label='usage')
ax4.legend()


#plt.subplot(4,1,3)
#plt.plot(ests)
#plt.ylabel('est (kbps)')
#plt.scatter(dss, dts)

#plt.subplot(4,1,4)
#plt.scatter(dss, dts, hist_res)

print 'show'
plt.show()
